package cn.myfirefly.moming.common.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.entity.MMDataDict;
import cn.myfirefly.moming.common.model.DataDictDto;
import cn.myfirefly.moming.common.model.DataDictFilterDto;
import cn.myfirefly.moming.common.service.DataDictItemService;
import cn.myfirefly.moming.common.service.DataDictService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;

@Controller
@RequestMapping("dataDict")
public class DataDictController extends BaseController {
	
	@Autowired
	DataDictService dataDictService; 
	
	@Autowired
	DataDictItemService dataDictItemService; 
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "datadict/index";
	}
	
	@RequestMapping("item/index.do")
	public String indexItem(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("dataTypeItem", request.getParameter("dataTypeItem"));
		request.setAttribute("dataShowTypeItem", request.getParameter("dataShowTypeItem"));
		return "datadict/item/index";
	}
	
	@RequestMapping("item/edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "datadict/item/edit";
	}
	
	@RequestMapping("item/add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("dataType", request.getParameter("dataType"));
		request.setAttribute("dataShowType", request.getParameter("dataShowType"));
		return "datadict/item/edit";
	}
	
	@RequestMapping("item/save.do")
	@ResponseBody
	public ResultDto save(DataDictDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = dataDictItemService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<DataDictDto> list(DataDictFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<DataDictDto> result = dataDictService.getList(dto);
		return result;
	}
	
	@RequestMapping("item/list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<MMDataDict> listItem(DataDictFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String dataTypeItem = request.getParameter("dataTypeItem");
		PageResultDto<MMDataDict> result = dataDictItemService.getList(dto,dataTypeItem);
		return result;
	}
	
	
	@RequestMapping("item/getDataDictItem.do")
	@ResponseBody
	public ResultDto getDataDict(HttpServletRequest request, 
			HttpServletResponse response) {
		String dataDictSn = request.getParameter("dataDictSn");
		DataDictDto dictDto = dataDictItemService.getDataDict(Integer.valueOf(dataDictSn));
		List<DataDictDto> list = new ArrayList<DataDictDto>();
		list.add(dictDto);
		return success(list);
	}
	
	@RequestMapping("item/del.do")
	@ResponseBody
	public ResultDto del(DataDictDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = dataDictItemService.del(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
}
